package com.example.train_operate.controller;

import com.example.train_operate.util.Result;
import com.example.train_operate.ws.manager.RoomManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Api(tags = "信令服务接口")
@RequestMapping("/signaling")
public class SignalingController {

    @Resource
    private RoomManager roomManager;

    @ApiOperation("获取房间信息")
    @GetMapping("/room/{roomId}")
    public Result getRoomInfo(@PathVariable String roomId){
        Map<String, Object> roomInfo = new HashMap<>();
        roomInfo.put("roomId", roomId);
        roomInfo.put("exists", roomManager.roomExists(roomId));
        roomInfo.put("size", roomManager.getRoomSize(roomId));
        roomInfo.put("users", roomManager.getRoomUsers(roomId));
        return Result.ok(roomId);
    }


    @ApiOperation("获取房间用户列表")
    @GetMapping("/room/{roomId}/users")
    public Result getRoomUsers(@PathVariable String roomId){
        return Result.ok(roomManager.getRoomUsers(roomId));
    }

    @ApiOperation("获取ICE服务器配置")
    @GetMapping("/ice-servers")
    public Result getIceServers(){
        Map<String, Object> config = new HashMap<>();
        List<Map<String, Object>> iceServers = List.of(
                Map.of("urls", "stun:stun.l.google.com:19302"),
                Map.of("urls", "stun:stun1.l.google.com:19302"),
                Map.of("urls", "stun:stun2.l.google.com:19302")
        );
        config.put("iceServers", iceServers);
        return Result.ok(config);
    }

    @ApiOperation("清理过期会话")
    @PostMapping("cleanup")
    public Result cleanupSession(){
        roomManager.cleanupExpiredSessions();
        return Result.ok();
    }

}
